package com.rxyl.love.petdoctor.service;

import android.app.Service;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.util.Log;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.rxyl.love.petdoctor.util.SharedUtil;


/**
 * Created by Administrator on 2016/1/14 0014.
 */
public class LocationService extends Service {
    //定位相关
    private LocationClient mLocClient;
    private MyLocationListenner myLocationListenner;
    private MyLocationConfiguration.LocationMode mCurrentMode;
    private String cityName;

    private boolean isFirst = true;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mCurrentMode = MyLocationConfiguration.LocationMode.NORMAL;

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // 定位初始化
        myLocationListenner = new MyLocationListenner();
        mLocClient = new LocationClient(this);
        mLocClient.registerLocationListener(myLocationListenner);
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true); // 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(1000 * 360); // 间隔性定位
        option.setIsNeedAddress(true);
        mLocClient.setLocOption(option);
        mLocClient.start();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void unbindService(ServiceConnection conn) {
        super.unbindService(conn);
    }

    /**
     * 定位SDK监听函数
     */
    public class MyLocationListenner implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            // map view 销毁后不在处理新接收的位置
            if (location == null) {
                return;
            }

            /*tv.setText("\n地址：" + location.getAddrStr()
                    + "\n市：" + location.getAddress().city
                    + "\n区：" + location.getAddress().district
                    + "\n纬度Latitude：" + location.getLatitude()
                    + "\n经度Longitude：" + location.getLongitude());*/
            cityName = location.getAddress().city;
            SharedUtil.putString("latitude","" + location.getLatitude());
            SharedUtil.putString("longitude","" + location.getLongitude());
            Intent intent;
            if (isFirst) {
                //发送到首页
                intent = new Intent("mainMsg");
                isFirst = false;
            } else {
                //发送到城市页
                intent = new Intent("cityMsg");
            }
            intent.putExtra("cityName", cityName);
            intent.putExtra("latitude", location.getLatitude() + "");
            intent.putExtra("longitude", location.getLongitude() + "");
            sendBroadcast(intent);
        }

        public void onReceivePoi(BDLocation poiLocation) {
        }
    }


}
